/*
 * Copyright 2006-2007 S W McIlroy
 * Licensed  under the  Apache License,  Version 2.0  (the "License");
 * you may not use  this file  except in  compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed  under the  License is distributed on an "AS IS" BASIS,
 * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
 * implied.
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.mockfu;

import static org.mockfu.Mocks.*;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class JdbcTest
{
    static class ConnectionManager
    {
        private static ConnectionManager instance = new ConnectionManager();

        public static ConnectionManager instance()
        {
            return instance;
        }

        public Connection getConnection()
        {
            return null;
        }

        public void releaseConnection( Connection connection )
        {
        }
    }

    static class UserDao
    {

        public Map<Integer, String> getUsers() throws SQLException
        {
            Map<Integer, String> users = new HashMap<Integer, String>();

            Connection connection = ConnectionManager.instance().getConnection();
            try
            {
                String sql = "select * from user;";
                Statement statement = connection.createStatement();
                try
                {
                    for( ResultSet rs = statement.executeQuery( sql ) ; rs.next() ; )
                    {
                        users.put( rs.getInt( 1 ), rs.getString( 2 ) );
                    }
                }
                finally
                {
                    statement.close();
                }
            }
            finally
            {
                ConnectionManager.instance().releaseConnection( connection );
            }

            return users;
        }
    }

    private MockControl<ConnectionManager> connectionManager;
    private MockControl<Connection> connection;
    private MockControl<Statement> statement;

    @BeforeTest
    public void setUp()
    {
        connectionManager = MockControl.of( ConnectionManager.class );
        connection = MockControl.of( Connection.class );
        statement = MockControl.of( Statement.class );

        stub( connectionManager.expectsOnce().getConnection(), connection.mock() );
        connectionManager.expectsOnce().releaseConnection( same( connection.mock() ) );

        connectionManager.injectInto( ConnectionManager.class );
    }

    @AfterTest
    public void tearDown()
    {
        connectionManager.eject();
        connection.eject();
        statement.eject();
    }

    @Test
    public void test() throws SQLException
    {
        MockControl<ResultSet> resultSet = MockControl.of( ResultSet.class );

        String sql = "select * from user;";
        stub( connection.expectsOnce().createStatement(), statement.mock() );
        stub( statement.expectsOnce().executeQuery( sql ), resultSet.mock() );
        statement.expectsOnce().close();

        resultSet.beginSequence();

        stub( resultSet.expectsOnce().next(), true );
        stub( resultSet.expectsOnce().getInt( 1 ), 7 );
        stub( resultSet.expectsOnce().getString( 2 ), "frank" );

        stub( resultSet.expectsOnce().next(), true );
        stub( resultSet.expectsOnce().getInt( 1 ), 9 );
        stub( resultSet.expectsOnce().getString( 2 ), "harry" );

        stub( resultSet.expectsOnce().next(), false );

        resultSet.endSequence();

        Map<Integer, String> users = new UserDao().getUsers();
        assertThat( users.remove( 7 ), "frank" );
        assertThat( users.remove( 9 ), "harry" );
        assertThat( users.isEmpty() );

        connectionManager.verifyAll();
    }
}
